/*
 * ExplicaEditEntityView.java
 *
 * $Id: ExplicaEditEntityView.java,v 1.9 2009-10-27 10:09:06 mario Exp $
 */
package org.ceteca.explica.client.gui;

import java.util.HashMap;

import org.apache.log4j.Logger;
import org.ceteca.explica.client.gui.ExplicaGUIBuilder;
import org.ceteca.explica.core.common.MultiSelectionSubentityOT;
import org.ceteca.explica.core.installer.EntityLauncher;
import org.ceteca.explica.core.installer.SubentityOT;
import org.ceteca.javaglade.Gui;
import org.ceteca.javaglade.JavaGladeException;
import org.ceteca.javaglade.handlers.GenericHandler;
import org.ceteca.javaglade.toolkit.swt.SwtDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import java.util.Vector;

public class ExplicaEditEntityView extends GenericHandler {
	private Logger logger;
	private String viewName;
	private SwtDialog dialog;
	private Gui parentGui;
	private String dialogName;
	private EntityLauncher entLauncher;
	
	/** Catalog of multi selection views */
	private HashMap<MultiSelectionSubentityOT, ExplicaDetailEntityView> entMultiSelViews;
	
	private Vector<ExplicaEventListener> listeners = new Vector<ExplicaEventListener>();
	
	/**
	 * Entity name of the corresponding view that is built.
	 */
	private String entityName;
	/**
     * Message for logging the entity glade configuration file path.
     */
    private static final String EXPLICA_LOG_ENTITYEDITION_GLADEPATH = "The entity (%s) edition interface configuration file has been found at this path: %s";
	
	public ExplicaEditEntityView(EntityLauncher entLauncher, Logger logger, String viewName, Gui parentGui, String dialogName) {
		this.entLauncher = entLauncher;
		this.logger = logger;
		this.viewName = viewName;
		this.parentGui = parentGui;
		this.dialogName = dialogName;
		this.gui = null;
		this.entityName = entLauncher.getEntity().getName();
	}
	
	public void buildExplicaEditEntityView(String gladepath) {
		this.logger.info(String.format(EXPLICA_LOG_ENTITYEDITION_GLADEPATH, this.entityName, gladepath));
		try {
			this.gui = ExplicaGUIBuilder.getInstance().buildGUI(this.viewName, gladepath, this.parentGui, this.parentGui.getParentWindow());
			this.logger.debug("EDIT ENTITY GUI1: " + this.getGui());
			this.logger.debug("EDIT ENTITY GUI1: " + this.gui);
		}
		catch (JavaGladeException ex) {
			ex.printStackTrace();
			this.gui = null;
			this.logger.debug("EDIT ENTITY GUI2: " + this.getGui());
		}
		
		this.logger.info(String.format("<---- Entity javaglade built: %s ---->", this.entityName));
		ExplicaGUIBuilder.getInstance().printGUIs(this.logger);
		this.logger.info(String.format("</--- Entity javaglade built: %s ---\\>", this.entityName));
		
		// Save the entity dialog instance for latter use.
		if (this.gui != null) {
			this.logger.info(String.format("ExplicaEditEntityView built: %s --> %s\nDIALOG: %s --> %s", this.entityName, this.viewName, 
					this.dialogName, this.gui.getDialog(this.dialogName)));
			this.dialog = this.gui.getDialog(this.dialogName);
			this.dialog.getShell().setMinimumSize(800, 600);
			this.dialog.getShell().setSize(800, 600);
//			this.dialog.getShell().setBackground(ExplicaGUIBuilder.getInstance().getColor(SWT.COLOR_BLUE));
			this.gui.setRootWidget(this.dialogName);
		}
		
		this.logger.debug("EDIT ENTITY GUI3: " + this.getGui());
	}
	
	public void showEditEntityView() {
		this.dialog.open();
	}
	
	public void clearEditEntityView() {
		
	}
	
	public void closeEditEntityView() {
		this.dialog.close();
	}
	
	public void dispose() {
		this.dialog.dispose();
	}
	
	public void on_entityEditionSaveButton_clicked(Event e) {
		Long selectedId = this.entLauncher.getSelectedId();
		this.logger.info(String.format("Entity (%s) edition saved: %s!",
				this.entLauncher.getEntity().getName(), selectedId == null ? "NEW REGISTRY"
						: selectedId.toString()));

		this.entLauncher.saveEntityRegistry();
	}
	
	public void on_entityEditionUndoButton_clicked(Event e) {
		Long selectedId = this.entLauncher.getSelectedId();
		this.logger.info(String.format("Entity (%s) edition undone: %s!",
				this.entLauncher.getEntity().getName(), selectedId == null ? "NEW REGISTRY"
						: selectedId.toString()));
		
		this.entLauncher.undoEntityEdition();
	}

	public void on_entityEditionCancelButton_clicked(Event e) {
		Long selectedId = this.entLauncher.getSelectedId();
		this.logger.info(String.format("Entity (%s) edition cancelled: %s!",
				this.entLauncher.getEntity().getName(), selectedId == null ? "NEW REGISTRY"
						: selectedId.toString()));

		this.entLauncher.cancelEntityEdition();
	}
	
	public SwtDialog getDialog() {
		this.logger.debug(String.format("The entity %s edition dialog (%s) has been found: %s", 
				this.entityName, this.dialogName, this.dialog));
		return this.dialog;
	}
	
	/**
	 * Fills the widgets of the entity edition view with the contents
	 * of the selected registry of the shown entity.
	 */
	public void updateView(){
		if (this.entLauncher.getSelectedEntity()==null)
			return;
		this.entLauncher.fillEntityData();
//		showSubentityMultiSelection();
		this.dialog.getShell().layout(true);
	}
	
	public ExplicaDetailEntityView getMultiSelectEntityView(String subentityKey) {
		ExplicaDetailEntityView result = null;
		if (this.entMultiSelViews!=null)
		for (SubentityOT subE : this.entMultiSelViews.keySet())
			if (subE.getSubentityData().getKey().equals(subentityKey))
				result = this.entMultiSelViews.get(subE);

		return result;
	}
	
	
	public void showSubentityMultiSelection() {
		if (this.entMultiSelViews!=null)
		for (ExplicaDetailEntityView entView : this.entMultiSelViews.values()) {
			entView.showEntityView();
		}
	}


	public void addMultiSelection(MultiSelectionSubentityOT subE,
			ExplicaDetailEntityView subentView) {
		if (this.entMultiSelViews==null)
			this.entMultiSelViews=new HashMap<MultiSelectionSubentityOT, ExplicaDetailEntityView>();
		this.entMultiSelViews.put(subE, subentView);
		
	}
	
	public void addExplicaEventListener(ExplicaEventListener l){
		listeners.add(l);
		
	
	}
	
	public void removeExplicaEventListener(ExplicaEventListener l){
		listeners.remove(l);
	}
	
	public void notifyEvent(int type){
		this.logger.debug("Disparando evento para "+listeners.size()+ " listeners");
		ExplicaEvent event=new ExplicaEvent(this, type);
		for (ExplicaEventListener l:listeners)
			l.explicaViewModified(event);
	};
	
	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: ExplicaEditEntityView.java,v 1.9 2009-10-27 10:09:06 mario Exp $";
	}
}

